<template>
  <el-dialog
    :title="!dataForm.username ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="密码20个字" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码20个字"></el-input>
    </el-form-item>
    <el-form-item label="头像可能很长所以255个字" prop="photo">
      <el-input v-model="dataForm.photo" placeholder="头像可能很长所以255个字"></el-input>
    </el-form-item>
    <el-form-item label="个性签名" prop="motto">
      <el-input v-model="dataForm.motto" placeholder="个性签名"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="nickname">
      <el-input v-model="dataForm.nickname" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="1为普通用户 2为活动发布者" prop="mask">
      <el-input v-model="dataForm.mask" placeholder="1为普通用户 2为活动发布者"></el-input>
    </el-form-item>
    <el-form-item label="用户邮箱用于找回密码" prop="email">
      <el-input v-model="dataForm.email" placeholder="用户邮箱用于找回密码"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          username: 0,
          password: '',
          photo: '',
          motto: '',
          nickname: '',
          mask: '',
          email: ''
        },
        dataRule: {
          password: [
            { required: true, message: '密码20个字不能为空', trigger: 'blur' }
          ],
          photo: [
            { required: true, message: '头像可能很长所以255个字不能为空', trigger: 'blur' }
          ],
          motto: [
            { required: true, message: '个性签名不能为空', trigger: 'blur' }
          ],
          nickname: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          mask: [
            { required: true, message: '1为普通用户 2为活动发布者不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '用户邮箱用于找回密码不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.username = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.username) {
            this.$http({
              url: this.$http.adornUrl(`/lovegoing_admin/user/info/${this.dataForm.username}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.password = data.user.password
                this.dataForm.photo = data.user.photo
                this.dataForm.motto = data.user.motto
                this.dataForm.nickname = data.user.nickname
                this.dataForm.mask = data.user.mask
                this.dataForm.email = data.user.email
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/lovegoing_admin/user/${!this.dataForm.username ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'username': this.dataForm.username || undefined,
                'password': this.dataForm.password,
                'photo': this.dataForm.photo,
                'motto': this.dataForm.motto,
                'nickname': this.dataForm.nickname,
                'mask': this.dataForm.mask,
                'email': this.dataForm.email
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
